package com.ai.service.security.impl;

import com.ai.common.CommonResponse;
import com.ai.entity.security.SysUserRoleRelation;
import com.ai.mapper.security.SysUserRoleRelationMapper;
import com.ai.model.param.SysUserRoleRelationParam;
import com.ai.service.security.ISysUserRoleRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户角色关联关系表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2023-04-04 19:46:07
 */
@Service
public class SysUserRoleRelationServiceImpl extends ServiceImpl<SysUserRoleRelationMapper, SysUserRoleRelation> implements ISysUserRoleRelationService {

    @Override
    public boolean removeByUserId(Integer id) {
       return this.remove(new QueryWrapper<SysUserRoleRelation>().eq("user_id",id));
    }

    /**
     * 更新角色用户关系
     * @param sysUserRoleRelationParam
     * @return
     */
    @Override
    public CommonResponse<String> updateRelation(SysUserRoleRelationParam sysUserRoleRelationParam) {
        SysUserRoleRelation sysUserRoleRelation = new SysUserRoleRelation();
        sysUserRoleRelation.setId(sysUserRoleRelationParam.getId());
        sysUserRoleRelation.setRoleId(Integer.valueOf(sysUserRoleRelationParam.getRoleId()));
        sysUserRoleRelation.setUserId(Integer.valueOf(sysUserRoleRelationParam.getUserId()));
        boolean r = this.updateById(sysUserRoleRelation);
        if (r){
            return  CommonResponse.ok("更新成功");
        }else {
            return CommonResponse.error("更新失败");
        }
    }

    /**
     * 新增角色关系表
     * @param sysUserRoleRelationParam
     * @return
     */
    @Override
    public CommonResponse<String> add(SysUserRoleRelationParam sysUserRoleRelationParam) {
        SysUserRoleRelation sysUserRoleRelation = new SysUserRoleRelation();
        sysUserRoleRelation.setId(sysUserRoleRelationParam.getId());
        sysUserRoleRelation.setRoleId(Integer.valueOf(sysUserRoleRelationParam.getRoleId()));
        sysUserRoleRelation.setUserId(Integer.valueOf(sysUserRoleRelationParam.getUserId()));
        boolean r = this.save(sysUserRoleRelation);
        if (r){
            return  CommonResponse.ok("新增成功");
        }else {
            return CommonResponse.error("新增失败");
        }
    }
}
